package nl.tudelft.jpacman.npc.ghost;

import nl.tudelft.jpacman.board.Direction;
import nl.tudelft.jpacman.board.Square;
import nl.tudelft.jpacman.level.Player;
import nl.tudelft.jpacman.npc.Ghost;
import nl.tudelft.jpacman.sprite.Sprite;
import java.util.Map;
import java.util.Optional;

public class Clyde extends Ghost {
    private static final int SHY_DISTANCE = 8;
    private static final int INTERVAL_VARIATION = 50;
    private static final int MOVE_INTERVAL = 250;

    public Clyde(Map<Direction, Sprite> spriteMap) {
        super(spriteMap, MOVE_INTERVAL, INTERVAL_VARIATION);
    }

    @Override
    public Optional<Direction> nextAiMove() {
        Unit nearest = Navigation.findNearest(Player.class, getSquare());
        if (nearest == null) return Optional.empty();

        Square target = nearest.getSquare();
        int distance = Navigation.getDistance(getSquare(), target);

        if (distance >= SHY_DISTANCE) {
            List<Direction> path = Navigation.shortestPath(getSquare(), target, this);
            if (path != null && !path.isEmpty()) {
                return Optional.of(path.get(0));
            }
        } else {
            return Navigation.randomMove(this);
        }
        return Optional.empty();
    }
}
